import sys
import os
import hashlib
import time


#工具类
class MuTool:
    def __init__(self):
        pass
    # 获取系统类型
    @staticmethod
    def get_os():
        nstype = sys.platform
        if nstype.startswith('win'):
            nType = 'win'
        elif nstype.startswith('linux'):
            nType = 'linux'
        elif nstype.startswith('darwin'):
            nType = 'mac'
        else:
            nType = 'other'
        return nType
    # 获取文件夹下的所有文件
    @staticmethod
    def get_path_file(folder_path:str):
        files = []
        # 判断文件夹是否存在
        if not os.path.exists(folder_path):
            return files
        # 遍历文件夹下的所有文件
        for file in os.listdir(folder_path):
            file_path = os.path.join(folder_path, file)
            # 判断是否是文件夹
            if os.path.isdir(file_path):
                files.extend(MuTool.get_path_file(file_path))
            else:
                files.append(file_path)
        return files
    # 删除文件或文件夹
    @staticmethod
    def delete_path(path:str):
        if not os.path.exists(path):
            return False
        # 判断是否是文件夹
        if os.path.exists(path):
            # 判断是否是文件夹
            if os.path.isdir(path):
                for file in os.listdir(path):
                    file_path = os.path.join(path, file)
                    MuTool.delete_path(file_path)
                os.rmdir(path)
            else:
                os.remove(path)
        return True
    # 判断数据是否为空
    @staticmethod
    def is_empty(data):
        return data == None or data == '' or data == [] or data == {} or data == set()
    # 获取当前时间
    @staticmethod
    def get_time(is_integer:bool=False, format:str='%Y-%m-%d %H:%M:%S'):
        import time
        if is_integer:
            return int(time.time())
        return time.strftime(format, time.localtime())
    #获取某一周的时间（start,end）
    @staticmethod
    def get_week_time(start_time:str|int|None=None,is_integer:bool=False, format:str='%Y-%m-%d %H:%M:%S'):
        from datetime import datetime, timedelta
        if not start_time:
            start_time = MuTool.get_time(is_integer=False, format=format)
        date = datetime.strptime(start_time, format)
        days_to_monday = date.weekday()
        if days_to_monday == 0:
            start_time = date
        else:
            start_time = date - timedelta(days=days_to_monday)
        end_time = start_time + timedelta(days=6)
        end_time = datetime.combine(end_time, datetime.max.time())
        start_time = datetime.combine(start_time, datetime.min.time())
        if is_integer:
            return int(start_time.timestamp()), int(end_time.timestamp())
        return start_time.strftime(format), end_time.strftime(format)
    #获取某一月的时间
    @staticmethod
    def get_month_time(start_time:str|int|None=None,is_integer:bool=False, format:str='%Y-%m-%d %H:%M:%S'):
        from datetime import datetime,timedelta
        if not start_time:
            start_time = MuTool.get_time(is_integer=False, format=format)
        date = datetime.strptime(start_time, format)
        start_time = datetime(date.year, date.month, 1)
        end_time = datetime(date.year, date.month, 1) + timedelta(days=32)
        end_time = datetime(end_time.year, end_time.month, 1) - timedelta(days=1)
        if is_integer:
            return int(start_time.timestamp()), int(end_time.timestamp())
        return start_time.strftime(format), end_time.strftime(format)
    #获取md5加密
    @staticmethod
    def get_md5(data=None):
        if MuTool.is_empty(data):
            data = int(time.time()*1000)
        data = str(data).encode('utf-8')
        return hashlib.md5(data).hexdigest()
    #获取文件后缀
    @staticmethod
    def get_file_suffix(data:str):
        array = data.split('.')
        return array[-1]
